package com.tlgen.controller.technology;

import com.tlgen.model.Department;
import com.tlgen.model.Enroll;
import com.tlgen.model.RespBean;
import com.tlgen.model.RespPageBean;
import com.tlgen.service.EnrollService;
import com.tlgen.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author: tlgen
 * @Discription:
 */
@RestController
@RequestMapping("/technology/enroll")
public class EnrollController {

    @Autowired
    EnrollService enrollService;

    @GetMapping("/")
    public RespPageBean getEnrollByPage(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer size,
                                        Enroll Enroll) {
        return enrollService.getEnrollByPage(page, size, Enroll);
    }

    @PostMapping("/")
    public RespBean addEnroll(@RequestBody Enroll Enroll) {
        if (enrollService.addEnroll(Enroll) == 1) {
            return RespBean.ok("添加成功!");
        }
        return RespBean.error("添加失败!!");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteEnrollByEid(@PathVariable Integer id) {
        if (enrollService.deleteEnrollByEid(id) == 1) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

    @PutMapping("/")
    public RespBean updateEnroll(@RequestBody Enroll Enroll) {
        if (enrollService.updateEnroll(Enroll) == 1) {
            return RespBean.ok("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    //poi接口 导出
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData() {
        //先得到要导出的数据嘛 这哩的page size可以为空是 在sql语句中<c:if 才能生效
        List<Enroll> list = (List<Enroll>) enrollService.getEnrollByPage(null, null,
                null).getData();
        return POIUtils.Enroll2Excel(list);
    }

    //导入 getAllDepartmentsWithOutChildren 是获取部门的时候不要哩边的children
    @PostMapping("/import")
    public RespBean importData(MultipartFile file) {
        List<Enroll> list = POIUtils.excel2Enroll(file);
        //数据插入到数据库中
        if (enrollService.addEnrolls(list) == list.size()) {
            return RespBean.ok("导入成功!");
        }
        return RespBean.error("导入失败!");
    }
}
